// pages/login/password.js
var app = getApp();
var api = require('../../api.js');
var util = require('../../utils/util.js');
var myreg = /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '18628681312',
    lev1_name: '',
    lev2_name: '',
    lev3_name: '',
    password: '',
    role: ["角色选择","县","支局","网格"],
    role_id:null,
    index1:0
  },
  get_role:function(e){
    var index = e.detail.value,
      role = this.data.role[index];
      
    this.setData({
      index1: index
    })
    if (role=="县"){
      this.setData({
        role_id: 13
      })
    } else if (role == "支局"){
      this.setData({
        role_id: 7
      })
    } else if (role == "网格") {
      this.setData({
        role_id: 12
      })
    }else{
      this.setData({
        role_id: null
      })
    }
  },

  formSubmit: function (e) {
    var info = e.detail.value;
    this.setData({
      phone: info.phone,
      password: info.password,
    })

    if (!myreg.test(info.phone)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none'
      })
    } else if (info.password.length < 6) {
      wx.showToast({
        title: '请输入您正确的初始密码',
        icon: 'none'
      })
    }else{
      if (this.data.role_id == null) {
        wx.showToast({
          title: '请选择您的角色',
          icon: 'none'
        })
      } else if (this.data.role_id == 13) {
        this.setData({
          lev1_name: info.lev1_name,
        })
        if (info.lev1_name.length < 1) {
          wx.showToast({
            title: '请输入您的所属县',
            icon: 'none'
          })
        }

      } else if (this.data.role_id == 7) {
        this.setData({
          lev1_name: info.lev1_name,
          lev2_name: info.lev2_name,
        })
        if (info.lev1_name.length < 1) {
          wx.showToast({
            title: '请输入您的所属县',
            icon: 'none'
          })
        } else if (info.lev2_name.length < 1) {
          wx.showToast({
            title: '请输入您的所属支局',
            icon: 'none'
          })
        }
      } else if (this.data.role_id == 12) {
        this.setData({
          lev1_name: info.lev1_name,
          lev2_name: info.lev2_name,
          lev3_name: info.lev3_name,
        })
        if (info.lev1_name.length < 1) {
          wx.showToast({
            title: '请输入您的所属县',
            icon: 'none'
          })
        } else if (info.lev2_name.length < 1) {
          wx.showToast({
            title: '请输入您的所属支局',
            icon: 'none'
          })
        } else if (info.lev3_name.length < 1) {
          wx.showToast({
            title: '请输入您的所属组织',
            icon: 'none'
          })
        }
      }  
      app.ajax(api.forget_password, {
        phone: this.data.phone,
        role_id: this.data.role_id,
        lev1_name: this.data.lev1_name,
        lev2_name: this.data.lev2_name,
        lev3_name: this.data.lev3_name,
        password_mw: this.data.password,
      }, (res) => {
        console.log(res);
        if (res.result == 'success') {
          wx.setStorageSync("rd_session", res.rd_session);
          wx.setStorageSync('phone', info.phone);
          wx.navigateTo({
            url: '../login/password',
          })
        }
      });
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      phone: wx.getStorageSync('phone'),
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})